﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SService
{
    public class ProductinformationS : ProductinformationSI
    {
        private readonly WmssystemContext myDb;

        public ProductinformationS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }
        public async Task<List<Pr_Productinformation>> Qu()
        {
            return await myDb.Pr_Productinformation.ToListAsync();
        }
        //添加
        public Task<int> Add(Pr_Productinformation proAdd)
        {
            myDb.Pr_Productinformation.Add(proAdd);
            return myDb.SaveChangesAsync();
        }
        //模糊查询
        public Task<List<Pr_Productinformation>> FenYe(int pageSize, int currentPage, string Key = null, string Status = null)
        {
            var query = myDb.Pr_Productinformation.AsQueryable();

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Key))
            {
                query = query.Where(c => c.P_ProductCode.Contains(Key) || c.P_ProductName.Contains(Key));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.P_ProductStatus.Contains(Status));
            }
            query = query.Where(c => c.P_PIsDelete == true);
            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }
        //修改
        public Task<int> Upd(Pr_Productinformation proUpd)
        {
            myDb.Pr_Productinformation.Update(proUpd);
            return myDb.SaveChangesAsync();
        }
        //逻辑删除
        public async Task<int> UpdIsDelete(int id, bool isDeleted)
        {
            var entity = await myDb.Pr_Productinformation.FindAsync(id);
            // 更新实体的删除状态
            entity.P_PIsDelete = isDeleted;
            // 标记实体为已修改状态
            myDb.Entry(entity).State = EntityState.Modified;
            // 保存更改到数据库
            return await myDb.SaveChangesAsync();
        }
    }
}
